package com.example.moduleone.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.example.baselibrary.activity.BaseNewActivity;
import com.example.baselibrary.bean.PublicViewHolder;
import com.example.baselibrary.util.BaseResourceUtil;
import com.example.moduleone.R;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by PC on 2018/4/11.
 */
public class ModuleOneMainActivity extends BaseNewActivity {

    private ViewHolder viewHolder;

    @Override
    protected int initLayoutRes() {
        return R.layout.activity_main_module_one;
    }

    @Override
    protected PublicViewHolder initViewHolderObject() {
        viewHolder = new ViewHolder();
        return viewHolder;
    }

    @Override
    protected void baseOnClick(int viewId) {
        if (BaseResourceUtil.whetherResourceIDSame(viewId, R.id.tv_ammo_text_1)) {
            Toast.makeText(ModuleOneMainActivity.this, viewHolder.tv_ammo_text_1.getText(), Toast.LENGTH_SHORT).show();
        } else if (BaseResourceUtil.whetherResourceIDSame(viewId, R.id.tv_ammo_text)) {
            new Thread(new Runnable() {
                @Override
                public void run() {
//                    String url = "https://a1.easemob.com/1195170711115729/botong/chatfiles/a8750890-51db-11e8-96b2-730854ffd071";//jpeg
                    String url = "https://a1.easemob.com/1195170711115729/botong/chatfiles/164a6d60-525e-11e8-b008-cde772df8fef";//gif
                    getExtensionName(url);
                }
            }).start();
            finish();
        } else if (BaseResourceUtil.whetherResourceIDSame(viewId, R.id.tv_ammo_text_2)) {
            Intent intent = new Intent(this, ModuleOneTestActivity.class);
            startActivity(intent);
        }
    }

    private void getExtensionName(String strUrl) {
        String aaa = "11111111111111111111111";
        Log.e("image", aaa);
        BufferedInputStream bis = null;
        HttpURLConnection urlConnection = null;
        URL url = null;
        try {
            url = new URL(strUrl);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.connect();
            bis = new BufferedInputStream(urlConnection.getInputStream());
            aaa = HttpURLConnection.guessContentTypeFromStream(bis);
            bis.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != urlConnection) {
                urlConnection.disconnect();
            }
        }
        Log.e("image", aaa);
    }

    @Override
    protected void initView(@Nullable Bundle bundle) {
        super.initView(bundle);
        viewHolder.tv_ammo_text_1.setText("哈哈哈");
    }


    public class ViewHolder extends PublicViewHolder {
        TextView tv_ammo_text;

        TextView tv_ammo_text_1;

        TextView tv_ammo_text_2;
    }
}
